package com.hnu.highlynewapp.service.impl;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.UserEntity;
import com.hnu.highlynewapp.enums.StatusEnum;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.service.UserService;
import com.hnu.highlynewapp.vo.DepartmentInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.Query;

import com.hnu.highlynewapp.dao.DepartmentInfoDao;
import com.hnu.highlynewapp.entity.DepartmentInfoEntity;
import com.hnu.highlynewapp.service.DepartmentInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("departmentInfoService")
public class DepartmentInfoServiceImpl extends ServiceImpl<DepartmentInfoDao, DepartmentInfoEntity> implements DepartmentInfoService {

    @Resource
    private UserService userService;

    @Resource
    private ContactService contactService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DepartmentInfoEntity> page = this.page(
                new Query<DepartmentInfoEntity>().getPage(params),
                new QueryWrapper<DepartmentInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DepartmentInfoEntity> getNoCheckList() {
        List<DepartmentInfoEntity> list=baseMapper.selectList(new QueryWrapper<DepartmentInfoEntity>().eq("status", StatusEnum.STATU_ONE.getCode()));
        return list;
    }

    @Override
    @Transactional
    public void auth(DepartmentInfoVo departmentInfoVo) {
        DepartmentInfoEntity departmentInfoEntity= new DepartmentInfoEntity();

        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(departmentInfoVo, departmentInfoEntity);
        BeanUtils.copyProperties(departmentInfoVo, contactEntity);

        contactEntity.setAddress(departmentInfoVo.getContactAddress());
        contactEntity.setEmail(departmentInfoVo.getContactEmail());
        contactEntity.setName(departmentInfoVo.getContactName());
        contactEntity.setPosition(departmentInfoVo.getContactPosition());
        contactEntity.setPhone(departmentInfoVo.getContactPhone());
        Long i = contactService.saveAndCheckExist(contactEntity);
        departmentInfoEntity.setContactId(i);
        departmentInfoEntity.setStatus(StatusEnum.STATU_ONE.getCode());
        this.save(departmentInfoEntity);
        UserEntity user = new UserEntity();
        user.setUserId(departmentInfoVo.getUserId());
        user.setStatus(1);
        userService.updateById(user);
    }

    @Override
    @Transactional
    public void passCheck(Long departmentId) {
        DepartmentInfoEntity departmentInfoEntity=this.getById(departmentId);
        departmentInfoEntity.setStatus(StatusEnum.STATU_TWO.getCode());
        this.updateById(departmentInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(departmentInfoEntity.getUserId());
        userEntity.setType(2);
        userEntity.setStatus(2);
        userService.updateById(userEntity);
    }

    @Override
    @Transactional
    public void failCheck(Long departmentId) {
        DepartmentInfoEntity departmentInfoEntity=this.getById(departmentId);
        departmentInfoEntity.setStatus(StatusEnum.STATU_THREE.getCode());
        this.updateById(departmentInfoEntity);
        UserEntity userEntity = new UserEntity();
        userEntity.setUserId(departmentInfoEntity.getUserId());
        userEntity.setStatus(0);
        userService.updateById(userEntity);
    }

}